package com.enjoy.mjbaseframe.util;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Gson 数据处理工具类
 * Created by Administrator on 2018/2/28.
 */

public class GsonUtil {

    private Gson gson ;
    private GsonUtil(){
        gson = new Gson();
    }
    private static class Holder{
        private  static  final GsonUtil gsonUtil=new GsonUtil();
    }

    public static GsonUtil getInstance(){
        return GsonUtil.Holder.gsonUtil;
    }

    /**
     * 返回集合对象
     * @param json
     * @param type
     * @param <T>
     * @return
     */
    public <T> List<T> getList(String json, Class<T[]> type) {
        T[] list = new Gson().fromJson(json, type);
        return new ArrayList(Arrays.asList(list));
    }

    /**
     * 返回对象
     * @param t
     * @param result
     * @param <T>
     * @return
     */
    public <T> T getObj(Class<T> t,String result){
        T obj = gson.fromJson(result,t);
        return obj;
    }

    /**
     * javabean to json
     *
     * @param person
     * @return
     */
    public String javabeanToJson(Object person) {
        Gson gson = new Gson();
        String json = gson.toJson(person);
        return json;
    }



}
